package com.yy.bolg.controller.adminUser;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.entity.AdminUserBean;
import com.yy.bolg.request.AdminUserRequest;
import com.yy.bolg.resp.AdminUserResp;
import com.yy.bolg.service.AdminUserService;
import com.yy.bolg.util.CodeGenerateUtil;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 总工控制器
 * @author enmaoFu
 * @date 2024-10-31
 * RequestMapping("/adminUser") 是说明这个类的访问地址是 /adminUser
 */
@Api(tags = {"总工"})
@Controller
@RequestMapping(value = "/adminUser")
public class AdminUserController {

    @Autowired
    private AdminUserService service;// 自动载入Service对象

    /**
     * 登录
     * 服务器访问路径：http://localhost:8080/bolg/adminUser/signIn
     * 本地访问路径：http://localhost:8111/adminUser/signIn
     * 参数：
     * {"adminUserPhone":"15123000000","adminUserPwd":"123456"}
     */
    @ApiOperation(value = "登录", notes = "参数：{\"adminUserPhone\":\"15123000000\",\"adminUserPwd\":\"123456\"}")
    @RequestMapping(value = "/signIn",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String signIn(@RequestBody AdminUserRequest request){
        String result;

        AdminUserBean adminUserBean = new AdminUserBean();
        adminUserBean.setPsAdminUserPhone(request.getAdminUserPhone());
        adminUserBean.setPsAdminUserPwd(request.getAdminUserPwd());
        adminUserBean.setPsAdminUserState(1);

        try {
            AdminUserBean returnBean = service.signIn(adminUserBean);

            AdminUserResp resp = new AdminUserResp();
            resp.setAdminUserCode(returnBean.getPsAdminUserCode());
            resp.setAdminUserName(returnBean.getPsAdminUserName());
            resp.setAdminUserPhone(returnBean.getPsAdminUserPhone());
            resp.setAdminUserProvince(returnBean.getPsAdminUserProvince());
            resp.setAdminUserCity(returnBean.getPsAdminUserCity());
            resp.setAdminUserRegion(returnBean.getPsAdminUserRegion());
            resp.setAdminUserState(returnBean.getPsAdminUserState());
            resp.setAdminUserGrade(returnBean.getPsAdminUserGrade());
            resp.setAdminUserJurisdiction(returnBean.getPsAdminUserJurisdiction());

            Map map = new HashMap();
            map.put("data", resp);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "登录成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 退出
     * 服务器访问路径：http://localhost:8080/bolg/adminUser/signOut
     * 本地访问路径：http://localhost:8111/adminUser/signOut
     * 参数：
     * {"adminUserPhone":"15123000000"}
     */
    @ApiOperation(value = "退出", notes = "参数：{\"adminUserPhone\":\"15123000000\"}")
    @RequestMapping(value = "/signOut",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String signOut(@RequestBody AdminUserRequest request){
        String result;

        AdminUserBean adminUserBean = new AdminUserBean();
        adminUserBean.setPsAdminUserPhone(request.getAdminUserPhone());
        adminUserBean.setPsAdminUserState(2);

        try {
            service.signOut(adminUserBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "退出成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 暂停二级权限总工账号使用
     * 服务器访问路径：http://localhost:8080/bolg/adminUser/suspend
     * 本地访问路径：http://localhost:8111/adminUser/suspend
     * 参数：
     * {"adminUserPhone":"15123000000"}
     */
    @ApiOperation(value = "暂停二级权限总工账号使用", notes = "参数：{\"adminUserPhone\":\"15123000000\"}")
    @RequestMapping(value = "/suspend",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String suspend(@RequestBody AdminUserRequest request){
        String result;

        AdminUserBean adminUserBean = new AdminUserBean();
        adminUserBean.setPsAdminUserPhone(request.getAdminUserPhone());
        adminUserBean.setPsAdminUserJurisdiction(2);

        try {
            service.suspend(adminUserBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "暂停权限成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 恢复二级权限总工账号使用
     * 服务器访问路径：http://localhost:8080/bolg/adminUser/recovery
     * 本地访问路径：http://localhost:8111/adminUser/recovery
     * 参数：
     * {"adminUserPhone":"15123000000"}
     */
    @ApiOperation(value = "恢复二级权限总工账号使用", notes = "参数：{\"adminUserPhone\":\"15123000000\"}")
    @RequestMapping(value = "/recovery",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String recovery(@RequestBody AdminUserRequest request){
        String result;

        AdminUserBean adminUserBean = new AdminUserBean();
        adminUserBean.setPsAdminUserPhone(request.getAdminUserPhone());
        adminUserBean.setPsAdminUserJurisdiction(1);

        try {
            service.recovery(adminUserBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "恢复权限成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 添加二级权限总工账号
     * 服务器访问路径：http://localhost:8080/bolg/adminUser/addAdminUser
     * 本地访问路径：http://localhost:8111/adminUser/addAdminUser
     * 参数：
     * {"adminUserName":"刘德华","adminUserPhone":"15123059793","adminUserPwd":"123456","adminUserProvince":"重庆","adminUserCity":"重庆","adminUserRegion":"渝北区"}
     */
    @ApiOperation(value = "添加二级权限总工账号", notes = "参数：{\"adminUserName\":\"刘德华\",\"adminUserPhone\":\"15123059793\",\"adminUserPwd\":\"123456\",\"adminUserProvince\":\"重庆\",\"adminUserCity\":\"重庆\",\"adminUserRegion\":\"渝北区\"}")
    @RequestMapping(value = "/addAdminUser",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String addAdminUser(@RequestBody AdminUserRequest request){
        String result;

        AdminUserBean adminUserBean = new AdminUserBean();
        adminUserBean.setPsAdminUserCode(CodeGenerateUtil.getAdminUserCode());
        adminUserBean.setPsAdminUserName(request.getAdminUserName());
        adminUserBean.setPsAdminUserPhone(request.getAdminUserPhone());
        adminUserBean.setPsAdminUserPwd(request.getAdminUserPwd());
        adminUserBean.setPsAdminUserProvince(request.getAdminUserProvince());
        adminUserBean.setPsAdminUserCity(request.getAdminUserCity());
        adminUserBean.setPsAdminUserRegion(request.getAdminUserRegion());
        adminUserBean.setPsAdminUserState(2);
        adminUserBean.setPsAdminUserGrade(2);
        adminUserBean.setPsAdminUserJurisdiction(1);

        try {
            service.add(adminUserBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "添加成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 删除二级权限总工账号
     * 服务器访问路径：http://localhost:8080/bolg/adminUser/delete
     * 本地访问路径：http://localhost:8111/adminUser/delete
     * 参数：
     * {"adminUserPhone":"15123000000"}
     */
    @ApiOperation(value = "删除二级权限总工账号", notes = "参数：{\"adminUserPhone\":\"15123000000\"}")
    @RequestMapping(value = "/delete",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String delete(@RequestBody AdminUserRequest request){
        String result;

        AdminUserBean adminUserBean = new AdminUserBean();
        adminUserBean.setPsAdminUserPhone(request.getAdminUserPhone());

        try {
            service.del(adminUserBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "删除成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 获取二级总工账号信息
     * 服务器访问路径：http://localhost:8080/bolg/adminUser/getAdminUserInfo
     * 本地访问路径：http://localhost:8111/adminUser/getAdminUserInfo
     * 参数：
     * {"adminUserPhone":"15123000000"}
     */
    @ApiOperation(value = "获取二级总工账号信息",notes = "参数：{\"adminUserPhone\":\"15123000000\"}", response = AdminUserResp.class)
    @RequestMapping(value = "/getAdminUserInfo",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String getAdminUserInfo(@RequestBody AdminUserRequest request){
        String result;

        AdminUserBean adminUserBean = new AdminUserBean();
        adminUserBean.setPsAdminUserPhone(request.getAdminUserPhone());

        try {
            AdminUserBean returnBean = service.findOneByPhone(adminUserBean);
            if (null != returnBean) {
                AdminUserResp resp = new AdminUserResp();
                resp.setAdminUserCode(returnBean.getPsAdminUserCode());
                resp.setAdminUserName(returnBean.getPsAdminUserName());
                resp.setAdminUserPhone(returnBean.getPsAdminUserPhone());
                resp.setAdminUserProvince(returnBean.getPsAdminUserProvince());
                resp.setAdminUserCity(returnBean.getPsAdminUserCity());
                resp.setAdminUserRegion(returnBean.getPsAdminUserRegion());
                resp.setAdminUserState(returnBean.getPsAdminUserState());
                resp.setAdminUserGrade(returnBean.getPsAdminUserGrade());
                resp.setAdminUserJurisdiction(returnBean.getPsAdminUserJurisdiction());

                Map map = new HashMap();
                map.put("data", resp);
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "获取信息成功", map);
            } else {
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL,"暂无数据",new HashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL,e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 查询（分页）总工账号-支持区域条件查询-条件字段可空（分页相关除外）
     * 服务器访问路径：http://localhost:8080/bolg/adminUser/findAllByCondition
     * 本地访问路径：http://localhost:8111/adminUser/findAllByCondition
     * 参数：
     * {"adminUserRegion":"渝北区","pageNum":"1","pageSize":"5"}
     */
    @ApiOperation(value = "查询（分页）总工账号-支持区域条件查询-条件字段可空（分页相关除外）", notes = "参数：{\"adminUserRegion\":\"渝北区\",\"pageNum\":\"1\",\"pageSize\":\"5\"}", response = AdminUserResp.class)
    @RequestMapping(value = "/findAllByCondition",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String findAllByCondition(@RequestBody AdminUserRequest request){
        Page pageInfo = PageHelper.startPage(request.getPageNum(), request.getPageSize());
        String result;

        AdminUserBean adminUserBean = new AdminUserBean();
        adminUserBean.setPsAdminUserRegion(request.getAdminUserRegion());

        try {
            List<AdminUserResp> respList = new ArrayList<>();
            for(AdminUserBean returnBean : service.findAllByCondition(adminUserBean)){
                AdminUserResp resp = new AdminUserResp();
                resp.setAdminUserCode(returnBean.getPsAdminUserCode());
                resp.setAdminUserName(returnBean.getPsAdminUserName());
                resp.setAdminUserPhone(returnBean.getPsAdminUserPhone());
                resp.setAdminUserProvince(returnBean.getPsAdminUserProvince());
                resp.setAdminUserCity(returnBean.getPsAdminUserCity());
                resp.setAdminUserRegion(returnBean.getPsAdminUserRegion());
                resp.setAdminUserState(returnBean.getPsAdminUserState());
                resp.setAdminUserGrade(returnBean.getPsAdminUserGrade());
                resp.setAdminUserJurisdiction(returnBean.getPsAdminUserJurisdiction());
                respList.add(resp);
            }

            Map map = new HashMap();
            map.put("pageInfo", pageInfo);
            map.put("rows", respList);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }
}
